import pickle
import random
from copy import copy


def data_pre():
    dataname = 'restaurant15_acsa'
    with open('testdata/'+dataname+'_all_global_parse_fg.pkl', 'rb') as f:
        all_datas = pickle.load(f)

        word_feature_set = set()    #word型特征
        single_word_feature_set =set()    #只有一个变量的word型特征
        multi_word_feature_set = set()    #有很多个变量的word型特征
        for item in all_datas[3]:
            if item.name1 not in word_feature_set:
                word_feature_set.add(item.name1)
            else:
                multi_word_feature_set.add(item.name1)
        single_word_feature_set = [x for x in word_feature_set if x not in multi_word_feature_set]

        var_id_map = dict()   #维护一个id对应表 例如：{'B0074703CM_102_ANONYMOUS:6:0'：0}
        for id,item in enumerate(all_datas[0]):
            var_id_map[item.name] = id
        #处理features
        features=list()
        feature=dict()
        weight_elem = dict()
        feature_id = 0
        #先处理relation型特征:
        for item in all_datas[2]:
            feature['feature_id'] = feature_id
            feature['feature_type'] = 'binary_feature'
            feature['feature_name'] = item.rel_type
            key = (var_id_map[item.name1], var_id_map[item.name2])
            weight_value = [-2.0, -1] if item.rel_type == 'asp2asp_sequence_oppo' else [2.0,1]  #暂时把featurevalue设置为+-1
            weight_elem[key] = weight_value
            feature['weight'] = copy(weight_elem)
            features.append(copy(feature))
            feature_id += 1
            weight_elem.clear()
            feature.clear()
        #处理word型只出现一次的特征
        for name in single_word_feature_set:
            for item in all_datas[3]:
                if name == item.name1:
                    feature['feature_id'] = feature_id
                    feature['feature_type'] = 'unary_feature'
                    feature['feature_name'] = item.name1
                    key = var_id_map[item.name2]
                    weight_value = [0,1]  #word型feature的weightvalue都初始化为0
                    weight_elem[key] = weight_value
                    feature['weight'] = copy(weight_elem)
                    features.append(copy(feature))
                    feature_id += 1
                    weight_elem.clear()
                    feature.clear()
        #处理word型出现多次的特征
        for name in multi_word_feature_set:
            feature['feature_id'] = feature_id
            feature['feature_type'] = 'unary_feature'
            for item in all_datas[3]:
                if name == item.name1:
                    key = var_id_map[item.name2]
                    weight_value = [0, 1]  #word型feature的weightvalue都初始化为0
                    weight_elem[key] = weight_value
            feature['feature_name'] = name
            feature['weight'] = copy(weight_elem)
            features.append(copy(feature))
            feature_id += 1
            weight_elem.clear()
            feature.clear()
        #整理变量
        variables=list()
        variable=dict()
        feature_set = dict()
        for id,item in enumerate(all_datas[0]):
            variable['var_id'] = id
            variable['var_name'] = item.name
            variable['is_evidence'] = item.isEvidence
            if item.polarity is None:
                variable['is_easy'] = False
                variable['is_evidence'] = False
                variable['label'] = random.choice((0,1))   #如果是hard,就随机初始化label
            else:
                variable['is_easy'] = True
                variable['is_evidence'] = True
                variable['label'] = 1 if item.polarity == 'positive' else 0  #如果是easy，就设置为easy的标签
            variable['true_label'] = 1 if item.gold_polarity == 'positive' else 0
            variable['prior'] = item.prior
            for feature in features:
                for kv in feature['weight'].items():
                    if type(kv[0]) == tuple and id in kv[0]:
                        feature_set[feature['feature_id']] = [0,kv[1][1]]
                    elif id == kv[0]:
                        feature_set[feature['feature_id']] = [0,kv[1][1]]
            variable['feature_set'] = copy(feature_set)
            variables.append(copy(variable))
            variable.clear()
            feature_set.clear()

    with open('testdata/'+dataname+'_variables.pkl', 'wb') as v:
        pickle.dump(variables,v)
    with open('testdata/'+dataname+'_features.pkl', 'wb') as f:
        pickle.dump(features,f)


data_pre()